package com.yeyks.app.controller.base.other;

import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.utils.DateUtils;
import com.yeyks.commonReference.service.other.CouponInfoService;
import com.yeyks.other.dal.domain.CouponInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author fuchangshuai
 * @date 2019/3/26 5:13 PM
 */
@Api(tags = "优惠券接口", description = "优惠券")
@Slf4j
@RestController("appCouponInfoController")
@RequestMapping(value = "/app/coupon")
public class CouponInfoController {

    @Autowired
    private CouponInfoService couponInfoService;

    /**
     * 获取充值10元得两张50元券的截止日期
     *
     * @return
     */
    @ApiOperation(value = "获取充值10元得两张50元券的截止日期", notes = "获取充值10元得两张50元券的截止日期")
    @ApiImplicitParam(paramType = "query", name = "price", value = "充值的金额", dataType = "int")
    @RequestMapping(value = "/overTime/image", method = RequestMethod.POST)
    public AjaxResponseBody<String> couponOverTimeImage(@RequestParam(name = "price", defaultValue = "1000") Integer price) {
        CouponInfo couponInfo = couponInfoService.selectCouponByPayAmount(price);
        if (couponInfo == null) {
            throw new ServiceException("该优惠券不存在");
        }
        String overTime = DateUtils.formatYMDHMSStr(couponInfo.getValidEnd());
        return AjaxResponseBody.success(overTime);
    }

    /**
     * 获取是否有推广所用的优惠券
     *
     * @return
     */
    @ApiOperation(value = "获取是否有推广所用的优惠券", notes = "获取是否有推广所用的优惠券")
    @ApiImplicitParam(paramType = "query", name = "type", value = "type类型(7 推广优惠券, 8 大礼包券)", required = true, dataType = "int")
    @PostMapping(value = "/exist")
    public AjaxResponseBody<Boolean> exist(@RequestParam(name = "type") Integer type) {
        if (type == null) {
            throw new ServiceException("类型必填");
        }
        Boolean flag = couponInfoService.recommendExist(type);
        return AjaxResponseBody.success(flag);
    }
}
